package com.ylzinfo.jck.service.impl;

import com.ylzinfo.jck.common.Const;
import com.ylzinfo.jck.dao.Ab01AMapper;
import com.ylzinfo.jck.dao.Ab01LogMapper;
import com.ylzinfo.jck.dao.Ab01Mapper;
import com.ylzinfo.jck.domain.Ab01;
import com.ylzinfo.jck.domain.Ab01A;
import com.ylzinfo.jck.domain.Ab01Log;
import com.ylzinfo.jck.service.Ab01aService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sunqy
 * @create 2019/11/20
 */
@Service
public class Ab01aServiceImpl implements Ab01aService {

    private Logger logger = LoggerFactory.getLogger(Ab01aServiceImpl.class);

    @Autowired(required = false)
    private Ab01AMapper ab01AMapper;

    @Autowired(required = false)
    private Ab01Mapper ab01Mapper;

    @Autowired(required = false)
    private Ab01LogMapper ab01LogMapper;

    @Override
    public int addAb01A(Ab01A ab01A) {
        ab01A.setState("0");
        return ab01AMapper.insert(ab01A);
    }

    @Override
    public int addAb01As(List<Ab01A> list) {
        return ab01AMapper.insertForeach(list);
    }

    /**
     * 定时任务，将各业务系统同步过来的ab01a中的数据经过处理同步到ab01中
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dealAb01aToAb01(Ab01A ab01A) {
        Ab01Log ab01Log = new Ab01Log();
        Ab01 ab01 = ab01Mapper.selectByAab003(ab01A.getAab003());
        if (ab01 == null) {//不存在，则新增
            ab01 = new Ab01();//如果这边没有new一下，下面copy的时候会报错
            BeanUtils.copyProperties(ab01A, ab01);
            ab01Mapper.insert(ab01);
        } else {//存在，则更新
            Long aab001 = ab01.getAab001();
            BeanUtils.copyProperties(ab01A, ab01);
            ab01.setAab001(aab001);//要以根据aab003查出来的aab001为准，防止aab001 被重置掉
            ab01Mapper.updateByPrimaryKeySelective(ab01);//用selective，没有值的不更新
        }
        //插入log表
        BeanUtils.copyProperties(ab01, ab01Log);
        ab01LogMapper.insert(ab01Log);
        //设置标志位
        ab01A.setState(Const.DealStatusEnum.OK.getCode());
        ab01A.setMsg(Const.DealStatusEnum.OK.getMsg());
    }
}
